package com.hmkj.web.controller.find;

import com.hmkj.common.entity.ResultEntity;
import com.hmkj.common.page.BasePageParams;
import com.hmkj.common.page.Pages;
import com.hmkj.core.mapper.operate.AnnounceMapper;
import com.hmkj.core.po.operate.Announce;
import com.hmkj.core.po.user.User;
import com.hmkj.core.service.operate.AgentService;
import com.hmkj.core.service.operate.AnnounceService;
import com.hmkj.web.controller.GenericController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@Api(description = "公告相关")
@Slf4j
@RestController
@RequestMapping(GenericController.BASE_API_URL + "/find")
public class AnnounceController extends GenericController<Integer, Announce> {

    @Resource
    private AnnounceService announceService;
    @Resource
    private AgentService agentService;

    @ApiOperation(value = "获取公告信息", notes = "获取公告信息")
    @ResponseBody
    @RequestMapping(value = "getAnnounceList", method = RequestMethod.POST)
    public ResultEntity<Pages<Announce>> getAnnounceList( @ModelAttribute BasePageParams basePageParams) {
        Map<String, Object> param = new HashMap<>();
        User user = getLoginUser();
        if(null == user){
            param.put("agentId", "");
        }else{
            param.put("agentId",user.getAgentId());
        }
        param.put("status", Announce.STATUS.PUBLISHED.code);
        param.put("basePageParams", basePageParams);
        Pages<Announce> pages = announceService.selectByPage(basePageParams,
                p -> ((AnnounceMapper) p).findCountForAnnounceList(param),
                p -> ((AnnounceMapper) p).findListForAnnounceList(param));
        return ok(pages);
    }


}
